package com.cloudm.technician.ui.modules.cloudm_box.channel_list

import android.view.View
import android.view.ViewGroup
import com.cloudm.technician.R
import com.cloudm.technician.data.model.ChannelInfo
import com.cloudm.technician.extensions.inflate
import com.cloudm.technician.extensions.loadImage
import com.cloudm.technician.ui.base.BaseAdapter
import com.cloudm.technician.ui.base.BaseViewHolder
import kotlinx.android.synthetic.main.item_channel_list.view.*

/**
 * 作者: wesley
 * 时间: 2017/12/23
 * 描述: 适配器
 */
class ChannelListAdapter : BaseAdapter<ChannelInfo>() {

    override fun onCreateViewHolder(parent: ViewGroup?,
                                    viewType: Int): BaseViewHolder<ChannelInfo> = ItemViewHolder(
            parent?.inflate(R.layout.item_channel_list))

    class ItemViewHolder(itemView: View?) : BaseViewHolder<ChannelInfo>(itemView) {

        override fun onBind(t: ChannelInfo) {
            val logo = t.logo
            val title = t.title
            val desc = t.description
            itemView.ivItemLogo.loadImage(logo)

            if (title == null || title.isNullOrEmpty()) {
                itemView.tvItemTitle.visibility = View.GONE
            } else {
                itemView.tvItemTitle.visibility = View.VISIBLE
                itemView.tvItemTitle.text = title
            }

            if (desc == null || desc.isNullOrEmpty()) {
                itemView.tvItemDesc.visibility = View.GONE
            } else {
                itemView.tvItemDesc.visibility = View.VISIBLE
                itemView.tvItemDesc.text = desc
            }
        }
    }
}